package com.alks.function.service.impl.stockdept.pcfactoryquery;

import com.alks.common.aop.AutoPageAop;
import com.alks.common.data.thread.UserIdThread;
import com.alks.common.utils.beanutils.BeanUtil;
import com.alks.entity.data.entity.PcSurplusDetail;
import com.alks.entity.data.response.ResponseInfo;
import com.alks.function.data.dto.stockdept.pcfactoryquery.SurplusIoDto;
import com.alks.function.data.request.stockdept.pcfactoryquery.SurplusIoRequest;
import com.alks.function.mapper.stockdept.pcfactorystockquery.SD_SQ_PcSurplusDetailMapper;
import com.alks.function.service.stockdept.pcfactoryquery.ISurplusService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import jodd.util.StringUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author MS
 * @since 2024/8/12下午3:10
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class SurplusServiceImpl extends ServiceImpl<SD_SQ_PcSurplusDetailMapper, PcSurplusDetail> implements ISurplusService {
    private final SD_SQ_PcSurplusDetailMapper detailMapper;

    @Override
    @AutoPageAop
    public ResponseInfo temp(SurplusIoRequest request, String slipType) {
        String companyId = UserIdThread.get().getCompanyId();
        /*获取数据*/
        List<PcSurplusDetail> details = lambdaQuery()
                .like(StringUtil.isNotBlank(request.getSlipNo()), PcSurplusDetail::getSlipNo, request.getSlipNo())
                .like(StringUtil.isNotBlank(request.getLotNo()), PcSurplusDetail::getLotNo, request.getLotNo())
                .eq(PcSurplusDetail::getSlipType, slipType)
                .eq(StringUtil.isNotBlank(request.getDeptNo()), PcSurplusDetail::getDeptNo, request.getDeptNo())
                .like(StringUtil.isNotBlank(request.getMtlName()), PcSurplusDetail::getMtlName, request.getMtlName())
                .eq(StringUtil.isNotBlank(request.getSlipReason()), PcSurplusDetail::getSlipReason, request.getSlipReason())
                .like(StringUtil.isNotBlank(request.getSysUser()), PcSurplusDetail::getSysUser, request.getSysUser())
                .like(StringUtil.isNotBlank(request.getMtlType()), PcSurplusDetail::getMtlNo, request.getMtlType())
                .ge(request.getSlipDateStart() != null, PcSurplusDetail::getSlipDate, request.getSlipDateStart())
                .le(request.getSlipDateStart() != null, PcSurplusDetail::getSlipDate, request.getSlipDateStart())
                .eq(PcSurplusDetail::getCompanyId,companyId).list();
        Page<PcSurplusDetail> page=(Page<PcSurplusDetail>) details;
        /*封装返回*/
        List<SurplusIoDto> dtos = BeanUtil.copyList(details, SurplusIoDto.class);
        Map<String,Object> map=new HashMap<>(2);
        map.put("dto",dtos);
        map.put("total", page.getTotal());
        return ResponseInfo.ok(map);
    }

    @Override
    public ResponseInfo back(String slipType) {
        String companyId = UserIdThread.get().getCompanyId();
        /*获取部门数据*/
        List<PcSurplusDetail> deptList = lambdaQuery()
                .select(PcSurplusDetail::getDeptName, PcSurplusDetail::getDeptNo)
                .eq(PcSurplusDetail::getSlipType,slipType)
                .last("GROUP BY DEPT_NO").list();
        List<Map<String,String>> dept = deptList.stream()
                .filter(Objects::nonNull)
                .map(a->{Map<String,String> map =new HashMap<>();
                map.put("deptNo",a.getDeptNo());
                map.put("deptName",a.getDeptName());
                return map;})
                .collect(Collectors.toList());
        /*获取仓管员数据*/
        List<PcSurplusDetail> sysUserList = lambdaQuery()
                .select(PcSurplusDetail::getSysUser)
                .eq(PcSurplusDetail::getSlipType,slipType)
                .last("GROUP BY SYS_USER").list();
        List<String> sysUser = sysUserList.stream()
                .filter(Objects::nonNull)
                .map(PcSurplusDetail::getSysUser)
                .collect(Collectors.toList());
        /*获取出入库原因*/
        List<PcSurplusDetail> slipReasonList = lambdaQuery()
                .select(PcSurplusDetail::getSlipReason)
                .eq(PcSurplusDetail::getSlipType,slipType)
                .last("GROUP BY SLIP_REASON").list();
        List<String> slipReason = slipReasonList.stream()
                .filter(Objects::nonNull)
                .map(PcSurplusDetail::getSlipReason)
                .collect(Collectors.toList());
        /*封装返回*/
        Map<String,Object> map = new HashMap<>(3);
        map.put("sysUser",sysUser);
        map.put("dept",dept);
        map.put("slipReason",slipReason);
        return ResponseInfo.ok(map);
    }
}
